<?php

/***************************************************************\
* FILE:		sys/pages/downer.inc
* SECTION:	pages > items
* FUNCTION:	download a download
* USES:	
* INCLUDES:	/sys/itemfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
	if (!$_SESSION['user_id'])
		errorpage(6);
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	switch($action) {
		default:
			add_alert("unknown action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE;

	$where = "ITEM.item_id = {$PAGE['id']} AND \n\t";
	$where .= "ITEM.item_id = DOWNLOAD.item_id";

	$sql = db_query_select(array(array('*'),array('*')), array('ITEM','DOWNLOAD'), $where);
	$sqlresult = db_do_query($sql);

	if (db_num_rows($sqlresult) !=1)
		errorpage(404);

	$download = db_fetch_array($sqlresult);

	if ($download['deleted'])
		errorpage(404);

	db_do_query(db_query_update(array('downloads', 'member_since', 'last_visit'), array('downloads+1', 'member_since', 'last_visit'), "MEMBER", "user_id = {$_SESSION['user_id']}"));
	db_do_query(db_query_update(array('downloaded'), array('downloaded+1'), "DOWNLOAD", "item_id = {$PAGE['id']}"));

	redirect("/down/{$download['filename']}");
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {}

?>
